package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Dictionary;
import cn.wolfcode.crm.domain.DictionaryDetail;
import cn.wolfcode.crm.service.IDictionaryDetailService;
import cn.wolfcode.crm.service.IDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @author konki
 * @time   2018年6月19日 下午7:03:47
 * @email  konki@qq.com
 * @description 
 */
@Controller
@RequestMapping("dictionary")
public class DictionaryController {
	@Autowired
    IDictionaryService service;
	@Autowired
	IDictionaryDetailService detailService;

	@RequestMapping("")
	@PermissionName("字典首页")
	@RequiresPermissions("dictionary:index")
	public String index(){
		return "dictionary/list";
	}
	@RequestMapping("list")
	@PermissionName("字典列表")
	@RequiresPermissions("dictionary:list")
	@ResponseBody
	public List<Dictionary> list(){
		return service.selectAll();
	}

	@RequestMapping("update")
	@PermissionName("字典编辑")
	@RequiresPermissions("dictionary:update")
	@ResponseBody
	public JsonResult update(Dictionary dictionary){
		JsonResult jsonResult = new JsonResult();
		try {
			service.updateByPrimaryKey(dictionary);
		}catch (Exception e){
			e.printStackTrace();
			jsonResult.markMsg("编辑字典失败");
			return jsonResult;
		}
		return jsonResult;
	}

	@RequestMapping("save")
	@PermissionName("字典保存")
	@RequiresPermissions("dictionary:save")
	@ResponseBody
	public JsonResult save(Dictionary dictionary){
		JsonResult jsonResult = new JsonResult();
		try {
			service.insert(dictionary);
		}catch (Exception e){
			e.printStackTrace();
			jsonResult.markMsg("添加字典失败");
			return jsonResult;
		}
		return jsonResult;
	}

	@RequestMapping("delete")
	@PermissionName("字典删除")
	@RequiresPermissions("dictionary:delete")
	@ResponseBody
	public JsonResult save(Long id){
		JsonResult jsonResult = new JsonResult();
		try {
			service.deleteByPrimaryKey(id);
		}catch (Exception e){
			e.printStackTrace();
			jsonResult.markMsg("删除字典失败");
			return jsonResult;
		}
		return jsonResult;
	}

	//根据id返回字典明细
	@RequestMapping("queryDictionaryDetail")
	@ResponseBody
	public List<DictionaryDetail> queryCarsize(Long id){
		return detailService.selectByDictionaryId(id);
	}
}
